package com.gxar.quick.ar.server.base.facade;

import com.gxar.common.entity.Result;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.fusion.redis.api.lock.FusionRedissonLock;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksShareDeleteResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksShareInfoResponse;
import com.gxar.quick.ar.server.db.service.WorksService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/4/16 17:44
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WorksFacade {

    private final WorksService worksService;

    private final FusionRedissonLock lock;


    /**
     * 查询分享作品详情
     *
     * @param userProfile 用户
     * @param id          作品ID
     * @return 响应
     */
    public Result<WorksShareInfoResponse> getDetailByWorksId(UserProfile userProfile, String id) {
        lock.lock("works:share:" + userProfile.getId(), 30);
        try {
            return worksService.getDetailByWorksId(userProfile, id);
        } finally {
            lock.unlock("works:share:" + userProfile.getId());
        }
    }

    /**
     * 分享删除
     *
     * @param userProfile 用户ID
     * @param id          作品ID
     * @return 响应
     */
    public WorksShareDeleteResponse shareDelete(UserProfile userProfile, Long id) {
        lock.lock("works:share:" + userProfile.getId(), 30);
        try {
            return worksService.shareDelete(userProfile, id);
        } finally {
            lock.unlock("works:share:" + userProfile.getId());
        }
    }
}
